<?php

App::import('View','Theme');

class GigaView extends ThemeView {
	
	var $validBlocks = array('sidebar', 'header','debugInfo','footer','adminDashboard');
	
	var $blockContent = array();
	var $layoutStyle = 'simple';
	var $layoutParams = array();
	/** Add jquery $(document).ready blocks.Prevent form hassle for writing $(document).ready again**/
	var $jQBlock = array();
	/**
	 * Specifes other additional view path if we are using abstractView
	 * @var unknown_type
	 */
	var $additionalViewPath = null;
	function __construct($controller)
	{
		//for adding view path of plugin from which present controller is being inhertied
		$this->__passedVars[] = 'abstractView';
		parent::__construct($controller);
		if(isset($this->abstractView) && $this->abstractView)
			$this->addPluginToViewPath($this->abstractView);
	}

	function getPagingUrl()
	{
		$pass = $this->params['pass'];
		$named = $this->params['named'];
		unset($named['page'],$named['direction'],$named['sort']);
		return am($pass,$named);
	}
	
	/**
	 * Set layout styles (one column, two column etc..)
	 * Styles are only used when using 'default' layout.
	 * For custom layouts - discuss with me.
	 *
	 * @param unknown_type $content_for_layout
	 * @param unknown_type $layout
	 * @return unknown
	 */
	function renderLayout($content_for_layout, $layout = null)
	{
		if($this->jQBlock)
		{
			$script = "<script type='text/javascript'>$(document).ready(function(){\n";
			$script .= implode("\n",$this->jQBlock);
			$script .= '});</script>';
			$this->addScript('JQBLOCK',$script);
		}
		return parent::renderLayout($content_for_layout, $layout);
	}	
	
	function addJqBlock($block)
	{
		array_push($this->jQBlock,$block);
	}
	
	function getModel()
	{
		if(!is_null($this->model))
			return ClassRegistry::getObject($this->model);
		elseif(isset($this->params['models'][0]))
			return ClassRegistry::getObject($this->params['models'][0]);
	}
	/**
	 * Specifed plugin to view path;
	 * @param unknown_type $plugin
	 * @return unknown_type
	 */
	function addPluginToViewPath($plugin)
	{
		$pluginName = up(array_shift(explode('.',$plugin)));
		if(defined($pluginName."_VIEW_PATH_ADDED") == true)
			return;
		$viewPaths = Configure::read('viewPaths');
		if(defined($pluginName."_PATH"))
		{
			$pluginPath = constant($pluginName."_PATH").'views'.DS;
			$appPath = APP.'views'.DS.'plugins'.DS.low($pluginName).DS;
			if(is_dir($appPath))
			{
				$viewPaths[] = $appPath;
			}
			if(is_dir($pluginPath))
			{
				$viewPaths[] = $pluginPath; 
			}
			define($pluginName."_VIEW_PATH_ADDED",true);
			Configure::write('viewPaths',$viewPaths);
			$parts = explode('.',$plugin);
			if(count($parts) == 2)
			{
				$this->additionalViewPath = low($parts[1]);
			}
		}
	}
	
	function _missingView($file, $error = 'missingView') 
	{
		$action = str_replace('.ctp','', basename($file)); 
		if($this->additionalViewPath && $this->viewPath != $this->additionalViewPath)
		{
			$this->viewPath = $this->additionalViewPath;
			return $this->_getViewFileName($action);
		}
		return parent::_missingView($file, $error);
	}
	
	/**
	 * @section templates Templates
	 * 
	 * 'Templates' are light weight elements which can not call helpers or other heavy objects. 
	 * They should be used whenever application is required to have an end-user edit-friendly view element.
	 * 
	 * @todo Removal of PHP tags to support end user editing from admin.
	 * @return unknown_type
	 */
	function template($name, $params = array())
	{
		return $this->element($name, $params, false);
	}
}

?>